import Router from "koa-router";
import httpRequest from "@/utils/http";
import { Result } from "@/utils/result";
import { getResolutionByWidth } from "@/utils/bingImgResolution";

const bingBgImgRouter = new Router({ prefix: "/api/daily/bingbgimg" });

bingBgImgRouter.get("/", async (ctx, next) => {
    const result = await httpRequest.get<any, any, any>({
        baseURL: "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1"
    });

    const data = result.images[0];

    const { width, height } = ctx.request.query;
    const resolution = getResolutionByWidth(width);

    data.url = "https://cn.bing.com" + data.url.replace(/1920x1080/g, resolution);

    Result.ok(ctx, data);
});

export default bingBgImgRouter;
